import utils from "../../utils/util.js";

Page({
    data: {
      nameVal: '',
      telVal: '',
      args: {},
      roomNum: 8315
    },
    onLoad: function(options){
      var that = this;
      that.setData({
        roomType: options.roomType,
        checkInDate: options.adate,
        checkOutDate: options.ddate,
        days: utils.getDays(options.adate, options.ddate),
        _price: options.roomPrice,
        price: Number(options.roomPrice * (utils.getDays(options.adate, options.ddate)))
      })
    },
    onShow: function() {
      var that = this;
      that.setData({
        args: {
          roomType: that.options.roomType,
          checkInDate: that.options.adate,
          checkOutDate: that.options.ddate,
          roomNum: that.data.roomNum,
          days: utils.getDays(that.options.adate, that.options.ddate),
          price: Number(that.options.roomPrice * (utils.getDays(that.options.adate, that.options.ddate)))}
      })
    },
    nameInput: function(e) {
      this.setData({
        nameVal: e.detail.value 
      })
    },
    telInput: function (e) {
      this.setData({
        telVal: e.detail.value
      })
    },
    // 定义验证方法
    verify: function() {
      var that = this;
      var nameVal = that.data.nameVal.replace(/\s/g, '');
      if (!/^[^ ]+$/.test(nameVal) || nameVal == '') {
        wx.showToast({
          title: '请输入姓名~',
          icon: "none",
          duration: 800
        });
        that.setData({
          nameVal: ''
        })
        return false
      }
      var telVal = that.data.telVal.replace(/\s/g, '');
      if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(telVal)) {
        wx.showToast({
          title: '请输入正确的手机号码~',
          icon: "none",
          duration: 800
        });
        that.setData({
          telVal: ''
        })
        return false
      }
      return true;
    },
    goPay:function(){
      if(!this.verify())return false;
  
        wx.showToast({
          title: '支付成功！',
          icon: 'success',
          duration: 800
        });
        var _arg = JSON.stringify(this.data.args)
        setTimeout(function () {
          wx.navigateTo({
            url: '../orderDetail/orderDetail?args=' + _arg
          })
        }, 1200)
      }
      
      // 微信支付接口
      // wx.requestPayment({
      //   timeStamp: '', //时间戳从1970年1月1日00:00:00至今的秒数,即当前的时间
      //   nonceStr: '', //随机字符串，长度为32个字符以下。
      //   package: '', //统一下单接口返回的 prepay_id 参数值，提交格式如：prepay_id=*
      //   signType: 'MD5', //签名算法，暂支持 MD5
      //   paySign: '', //签名,具体签名方案参见小程序支付接口文档;
      //   success: function (res) { //接口调用成功的回调函数

      //   },
      //   fail: function (res) { //接口调用失败的回调函数

      //   }
      // })
  
})